using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectBinhLuanTinTuc()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectBinhLuanTinTucByIDTinTuc(int idtintuc, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDTinTuc = idtintuc,
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC,
				CurrentPage = page
			};
			return View("SelectBinhLuanTinTuc", CreateViewModel(data));
		}

		public ActionResult SelectBinhLuanTinTucByIDTinTucPaging(int idtintuc, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDTinTuc = idtintuc,
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListBinhLuanTinTuc", CreateViewModel(data));
		}


		public ActionResult DeleteBinhLuanTinTuc(int id, int page = 1)
		{
			var result = _repBinhLuanTinTuc.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC,
				CurrentPage = page
			};
			return View("SelectBinhLuanTinTuc", CreateViewModel(data));
		}

		public ActionResult EditBinhLuanTinTuc(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditBinhLuanTinTuc", CreateViewModel(data));
		}

		public ActionResult CancelEditingBinhLuanTinTuc(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListBinhLuanTinTuc", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateBinhLuanTinTuc(FormCollection forms)
		{
			string id = forms.GetValues("BinhLuanTinTuc_ID")[0];
			string idtintuc = forms.GetValues("BinhLuanTinTuc_IDTinTuc")[0];
			string noidung = forms.GetValues("BinhLuanTinTuc_NoiDung")[0];
			string tennguoibinhluan = forms.GetValues("BinhLuanTinTuc_TenNguoiBinhLuan")[0];
			string ngaybinhluan = forms.GetValues("BinhLuanTinTuc_NgayBinhLuan")[0];
			var binhluantintuc = _repBinhLuanTinTuc.SelectByID(int.Parse(id));
			binhluantintuc.IDTinTuc = int.Parse(idtintuc.Replace(".", "").Replace(",", ""));
			binhluantintuc.NoiDung = noidung;
			binhluantintuc.TenNguoiBinhLuan = tennguoibinhluan;
			binhluantintuc.NgayBinhLuan = DateTime.Now;
			var result = _repBinhLuanTinTuc.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC,
			};
			return View("SelectBinhLuanTinTuc", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertBinhLuanTinTuc(FormCollection forms)
		{
			string idtintuc = forms.GetValues("BinhLuanTinTuc_IDTinTuc")[0];
			string noidung = forms.GetValues("BinhLuanTinTuc_NoiDung")[0];
			string tennguoibinhluan = forms.GetValues("BinhLuanTinTuc_TenNguoiBinhLuan")[0];
			string ngaybinhluan = forms.GetValues("BinhLuanTinTuc_NgayBinhLuan")[0];
			var newItem = new BinhLuanTinTuc
			{
				IDTinTuc = int.Parse(idtintuc),
				NoiDung = noidung,
				TenNguoiBinhLuan = tennguoibinhluan,
				NgayBinhLuan = ngaybinhluan == "" ? DateTime.Now : DateTime.Parse(ngaybinhluan)
			};
			var result = _repBinhLuanTinTuc.Insert(newItem);
			var data = new DataTransferViewModel
			{
				IDTinTuc = int.Parse(idtintuc),
				EnumViewModelType = EnumViewModel.ADMIN_BINHLUANTINTUC,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectBinhLuanTinTuc", CreateViewModel(data));
		}


	}
}